<?php
namespace app\common\model;

use think\Model;

class Menu extends Model
{
    protected $pk     = 'id';
    protected $auto   = ['create_time', 'admin_id'];
    protected $insert = ['allow_del' => 1];

    protected function setCreateTimeAttr()
    {
        return time();
    }
    protected function setAdminIdAttr()
    {
        return 0; //Session::get('admin');
    }

    public function getMenu()
    {
        $menu = $this::order("sort", "desc")->select();
        if ($menu) {
            $menu = collection($menu)->toArray();
        }
        return $menu;
    }

    public function saveMenu($data, $where = false)
    {
        if ($where) {
            return $this::where($where)->update($data);
        } else {
            if ($this::save($data)) {
                return $this->id;
            }
        }
    }

    public function delMenu($id)
    {
        if ($id) {
            $where['id']=["in",$id];
            return $this::where($where)->where(["allow_del"=>1])->delete();
        }
    }
}
